package com.mcms.service.fz;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.mcms.util.*;
import javax.servlet.http.HttpServletRequest;

import org.ralasafe.user.User;

import com.mcms.service.common.DealResult;
import com.mcms.util.DBHelper;
import com.mcms.web.vo.DWZResult;

/**
 * 分诊台分诊业务类
 * @author 
 */
public class FZInfoService {
	public static DWZResult execute(Map sessionMap, Map paramMap) {

		boolean isSuc = true;
		FZInfoService fZInfoService = new FZInfoService();
		
		// 根据客户号和就诊号查询该记录是否已存在
		String realationId = fZInfoService.isHaveFZInfo(sessionMap, paramMap);
			
		if (realationId!=null && realationId.trim().length()>0) {// 如果该记录已存在，则UPDATE
			paramMap.put("realationId", realationId);// 将需要更新的主键传入
			isSuc = fZInfoService.updateFZInfo(sessionMap, paramMap);
		} else {// 否则，INSERT该记录

			isSuc = fZInfoService.insertFZInfo(sessionMap, paramMap);
		}

		// 装配结果对象
		paramMap.put("tabId", "aa");
		DWZResult dwzResult = DealResult.executeForMap(paramMap, isSuc);
		return dwzResult;
	}
	
	
	/**
	 * 判断当前此客户是否有分诊记录，有：true，否：false
	 * 
	 * @return
	 */
	private String isHaveFZInfo(Map sessionMap, Map paramMap){
		
		String doctorId = (String)paramMap.get("doctorId");// 客户预约就诊号
		String customId = (String)paramMap.get("customid");// 客户号ID
		Map resultMap = new HashMap();
		
		String sql = "select t1.id from MCMS_CUSTOM_DOCTOR_RALATION t1, MCMS_CUSTOM_DOCTOR t2 " +
				"where t1.custom_id = t2.custom_id and t1.doctor_id = t2.doctor_id and deal_time is not null";
		DBHelper dbh = DBHelper.getInstance();
		List list = dbh.query(sql);// 查询分诊后分配给了哪位医生（ID）
		
		String realationId = null;
		Integer deptId = null; 
		if(list.size()>0){
			Map<String, String> map = (Map<String, String>) list.get(0);
			
			realationId = map.get("id");
		}
		
		return realationId;
	}
	
	/**
	 * 根据客户号和就诊号更新该条分诊记录
	 * @return
	 */
	private boolean updateFZInfo(Map sessionMap, Map paramMap){
		paramMap.put("message", "分诊成功！");
		
		String remark = (String) paramMap.get("remark");// 分诊备注信息
		String dealDoctor = (String) paramMap.get("dealDoctor");// 处置医生ID
		String realationId = (String)paramMap.get("realationId");// 医生客户关系表
		if(realationId!=null && realationId.trim().length()>0 && dealDoctor!=null && dealDoctor.trim().length()>0){
			String sql = "update MCMS_CUSTOM_DOCTOR_RALATION set deal_doctor = '"+dealDoctor+"' ,remark='"+remark+"' where id ='"+realationId+"'";
			DBHelper dbh = DBHelper.getInstance();
			int result = dbh.execute(sql);
			if(result>0){
				return true;
			}
		}
		
		return false;
	}
	
	/**
	 * 插入新的分诊记录
	 * @return
	 */
	private boolean insertFZInfo(Map sessionMap, Map paramMap){
		paramMap.put("message", "分诊成功！");
		
		// 获取参数
		String remark = (String) paramMap.get("remark");// 分诊备注信息
		String dealDoctor = (String) paramMap.get("dealDoctor");// 处置医生ID
		String doctorId = (String)paramMap.get("doctorId");// 预约就诊号
		String customId = (String)paramMap.get("customid");// 当前被分诊客户ID
		
		User user = (User)sessionMap.get("mcmsUser");// 当前登录用户对象
		String userId = null;
		if(user!=null){
			userId = user.getId().toString();
		}
		
		// 先保存客户就诊表信息：
		String sql = "insert into MCMS_CUSTOM_DOCTOR(doctor_id,custom_id,create_id,create_time,remark,bargain_status,doctor_status) " +
				"values (?,?,?,?,?,?,?)";
		List list = new ArrayList();
		list.add(doctorId);
		list.add(customId);
		list.add(userId);
		list.add(CalendarUtil.getCurrentDateTime());
		list.add("初诊");
		list.add("1");// 1未成交
		list.add("3");// 3-已就诊
		DBHelper dbh = DBHelper.getInstance();
		int i = dbh.execute(sql, list);
		if(i>0){
			
		}else{
		   paramMap.put("message", "就诊表信息插入失败！");
		   return false;
		}
		
		
		// 再保存客户医生关系表信息 备注写初诊；就诊状态：已就诊；成交状态：未成交；是否过期：0；创建人：分诊人员ID，从session中取,处置记录ld为空即可，在生成处置信息时填写
		sql = "insert into MCMS_CUSTOM_DOCTOR_RALATION(id,custom_id,doctor_id,deal_doctor,deal_time,remark) " +
				" values (?,?,?,?,?,?)";
		
		list = new ArrayList();
		
		list.add(UUID.randomUUID().toString());
		list.add(customId);
		list.add(doctorId);
		list.add(dealDoctor);// 处置医生
		list.add(CalendarUtil.getCurrentDateTime());
		list.add(remark);
		dbh = DBHelper.getInstance();
		i = dbh.execute(sql, list);
		if(i>0){
		}else{
			paramMap.put("message", "客户医生关联信息插入失败！");
			return false;
		}
		return true;
	}
}
